package com.define.ssh.action.config;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.model.Config;
import com.define.ssh.service.ConfigService;

/**
 *  
 * 
 */
@ParentPackage("admin")
public class EnterpriseLogoAction extends BaseAdminAction {

	@Resource(name = "configServiceImpl")
	private ConfigService configService;

	private Config config21;// 企业名称
	private Config config22;// 企业LOGO

	private File logo;
    private String logoFileName;
	
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String index() {
		config21 = this.configService.get(21L);
		config22 = this.configService.get(22L);
		return "enterpriseLogo";
	}

	public String update() throws IOException {

		final Config temp21 = this.configService.get(config21.getId());
		temp21.setContent(config21.getContent());
		this.configService.update(temp21);

		if(logo!=null){
			String logopath="/images/logo.jpg";
			String realpath = ServletActionContext.getServletContext().getRealPath("/images");
	        if (logo != null) {
	            File savefile = new File(new File(realpath), "logo.jpg");
	            if (!savefile.getParentFile().exists())
	                savefile.getParentFile().mkdirs();
	            FileUtils.copyFile(logo, savefile);
	        }
	        final Config temp22 = this.configService.get(config22.getId());
			temp22.setContent(logopath);
			this.configService.update(temp22);
		}
		return index();
	}

	public Config getConfig21() {
		return config21;
	}

	public void setConfig21(Config config21) {
		this.config21 = config21;
	}

	public Config getConfig22() {
		return config22;
	}

	public void setConfig22(Config config22) {
		this.config22 = config22;
	}

	public File getLogo() {
		return logo;
	}

	public void setLogo(File logo) {
		this.logo = logo;
	}

	public String getLogoFileName() {
		return logoFileName;
	}

	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}

}